package com.spiro.test.embedding.jetty;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by tz0643 on 2016/5/26.
 */
public class HelloHandler extends AbstractHandler {

    final String greeting;

    public HelloHandler() {
        this.greeting = "Hello World!";
    }

    public HelloHandler(String greeting) {
        this.greeting = greeting;
    }

    @Override
    public void handle(String target,
                       Request baseRequest,
                       HttpServletRequest request,
                       HttpServletResponse response)
            throws IOException, ServletException {
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        response.getWriter().println("<h1>" + greeting + "</h1>");

    }
}
